﻿using System;
using System.Collections.Generic;
using System.Text;
using TransportAbstractionLibrary;
using TransportAbstractionLibrary.Exceptions;

namespace UDPServerTest
{
    class EchoConnection : IProtocolUpperLayer
    {
        Guid id = new Guid("779E3C64-091A-4b11-AF7F-7771B8E53ED6");

        IProtocolLowerLayer lowerLayer;

        #region IProtocolUpperLayer Members

        void IProtocolUpperLayer.Bind(IProtocolLowerLayer lowerLayer)
        {
            if (this.lowerLayer == null)
            {
                this.lowerLayer = lowerLayer;
            }
            else
            {
                throw new ArgumentException("Already bound!");
            }
        }

        void IProtocolUpperLayer.ConnectionClosed()
        {
            Console.WriteLine("Connection closed!");
        }

        public IProtocolLowerLayer LowerLayer
        {
            get { return lowerLayer; }
        }

        void IProtocolUpperLayer.OnException(TALException talException)
        {
            Console.WriteLine("OnException, Ex: " + talException.ToString());
        }

        void IProtocolUpperLayer.Receive(byte[] data)
        {
            Console.WriteLine("Data recived!, data.Length: " + data.Length);
            lowerLayer.Send(data);
        }

        #endregion

        #region IProtocolBase Members

        public Guid Id
        {
            get { return id; }
        }

        void IProtocolBase.Initialize()
        {
            Console.WriteLine("Initialize");
        }

        public LayerType LayerType
        {
            get
            {
                if (lowerLayer != null)
                {
                    return lowerLayer.LayerType | LayerType.Top;
                }
                else
                {
                    return LayerType.Top;
                }
            }
        }

        bool IProtocolBase.SuppressInitialization
        {
            get { return lowerLayer.SuppressInitialization; }
        }

        #endregion

        #region IDisposable Members

        public void Dispose()
        {
            lowerLayer.Dispose();
        }

        #endregion
    }
}
